//
//  MCTableCell.m
//  MClient
//
//  Created by  史正烨 on 10-8-25.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MCTableCell.h"
#import "MCControl.h"
#import "MCImageView.h"
@implementation MCTableCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // Initialization code
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)layoutSubviews
{
	[super layoutSubviews];
	for (UIView* view in self.contentView.subviews) {
		if ([view isKindOfClass:[MCControl class]]) {
			CGRect frame = view.frame;
			frame.size.height = self.bounds.size.height - frame.origin.y - 10;
			view.frame = frame;
		}
		if ([view isKindOfClass:[MCImageView class]]) {
			CGRect frame = view.frame;
			frame.size.height = self.bounds.size.height - frame.origin.y - 10;
			view.frame = frame;
		}
		if ([view isKindOfClass:[UIWebView class]]) {
			CGRect frame = view.frame;
			frame.size.height = self.bounds.size.height - frame.origin.y - 10;
			view.frame = frame;
		}
	}
}

- (void)dealloc {
    [super dealloc];
}


@end

#pragma mark -- MCTableCell -- 
@implementation MenuCell
@synthesize _menu;

#define SafeRelease(p) if (p) {\
[p release]; p = nil;\
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		self.selectionStyle = UITableViewCellSelectionStyleBlue;
		self.textLabel.backgroundColor = [UIColor clearColor];
		self.detailTextLabel.font = [UIFont systemFontOfSize:10];
		
		UILabel *labelField = [[UILabel alloc] initWithFrame:CGRectMake(272, 11, 30, 18)];
		labelField.textColor = [UIColor whiteColor];
		labelField.textAlignment = UITextAlignmentCenter;
		labelField.backgroundColor = [UIColor clearColor];
		labelField.font = [UIFont boldSystemFontOfSize:12.0];
		labelField.numberOfLines = 1;
		labelField.tag = -100;
		[self addSubview:labelField];
		[labelField release];
	}

    return self;
}
- (void)showCell:(NSString*)aTitle withIcon:(NSString*)aIcon menu:(NSMutableDictionary *)aMenu
{
	self.textLabel.textColor = [MCUserDefaults instance].skin_menuFontColor;
	self.textLabel.text = aTitle;
	self.textLabel.backgroundColor = [UIColor clearColor];
	self.imageView.image = [MCFunction scaleAndRotateImage:[UIImage imageNamed:aIcon] width:32.0 height:32.0];
	self.imageView.backgroundColor = [UIColor clearColor];
	self.detailTextLabel.text = [aMenu objectForKey:@"desc"];
	self.detailTextLabel.backgroundColor = [UIColor clearColor];
	
	self._menu = aMenu;
	if ([[self._menu objectForKey:@"showc"] boolValue] == YES) {
		if (![self._menu objectForKey:@"listCount"]) {
			SafeRelease(_jsonDao);
			_jsonDao = [[MServerJsonDAO alloc] init];
			NSMutableDictionary *url = [self._menu objectForKey:@"url"];
			[_jsonDao getListSizeAsync:[url objectForKey:@"dto"] == nil?@"":[url objectForKey:@"dto"]
							layoutName:[url objectForKey:@"lytn"] == nil?@"":[url objectForKey:@"lytn"]
						   queryFilter:[url objectForKey:@"qflt"] == nil?[[NSMutableArray alloc] init]:[url objectForKey:@"qflt"]
							  delegate:self];
			MClientAppDelegate *appDelegate = (MClientAppDelegate*)[[UIApplication sharedApplication] delegate];
			[appDelegate stopActivityIndicatorView];
		}else {
			UILabel *labelField = (UILabel *)[self viewWithTag:-100];
			labelField.text = [self._menu objectForKey:@"listCount"];
			[self bringSubviewToFront:labelField];
		}
	}
}

- (void) getDataFromServerSuccess: (NSMutableDictionary *)returnValue {
	UILabel *labelField = (UILabel *)[self viewWithTag:-100];
	UIImage *image;
	if ([(NSString *)returnValue intValue] < 10) {
		image = [UIImage imageNamed:@"badgeValue1.png"];
		labelField.frame = CGRectMake(268, 11, 30, 18);
	} else if ([(NSString *)returnValue intValue] >= 10 && [(NSString *)returnValue intValue] < 100) {
		image = [UIImage imageNamed:@"badgeValue2.png"];
		labelField.frame = CGRectMake(265, 11, 30, 18);
	} else if ([(NSString *)returnValue intValue] >= 100 && [(NSString *)returnValue intValue] < 1000){
		image = [UIImage imageNamed:@"badgeValue3.png"];
		labelField.frame = CGRectMake(262, 11, 30, 18);
	} else {
		image = [UIImage imageNamed:@"badgeValue3.png"];
		labelField.frame = CGRectMake(262, 11, 30, 18);
	}
	UIImageView *imageView = (UIImageView *)[self viewWithTag:-99];
	if (!imageView) {
		if ([(NSString *)returnValue intValue] < 10) {
			imageView = [[UIImageView alloc] initWithFrame:CGRectMake(274, 11, image.size.width, image.size.height)];
		} else if ([(NSString *)returnValue intValue] >= 10 && [(NSString *)returnValue intValue] < 100) {
			imageView = [[UIImageView alloc] initWithFrame:CGRectMake(268, 11, image.size.width, image.size.height)];
		} else if ([(NSString *)returnValue intValue] >= 100 && [(NSString *)returnValue intValue] < 1000){
			imageView = [[UIImageView alloc] initWithFrame:CGRectMake(261, 11, image.size.width, image.size.height)];
		} else {
			imageView = [[UIImageView alloc] initWithFrame:CGRectMake(261, 11, image.size.width, image.size.height)];
		}
		imageView.image = image;
		imageView.tag = -99;
		[self addSubview:imageView];
		[self bringSubviewToFront:imageView];
		[imageView release];		
	} else {
		if ([(NSString *)returnValue intValue] < 10) {
			imageView.frame = CGRectMake(274, 11, image.size.width, image.size.height);
		} else if ([(NSString *)returnValue intValue] >= 10 && [(NSString *)returnValue intValue] < 100) {
			imageView.frame = CGRectMake(268, 11, image.size.width, image.size.height);
		} else if ([(NSString *)returnValue intValue] >= 100 && [(NSString *)returnValue intValue] < 1000){
			imageView.frame = CGRectMake(261, 11, image.size.width, image.size.height);
		} else {
			imageView.frame = CGRectMake(261, 11, image.size.width, image.size.height);
		}
		imageView.image = image;
		[self bringSubviewToFront:imageView];
	}
	labelField.hidden = NO;
	imageView.hidden = NO;
	if ([(NSString *)returnValue intValue] <= 0) {
		labelField.hidden = YES;
		imageView.hidden = YES;
	}
	labelField.text = (NSString *)returnValue;
	[self bringSubviewToFront:labelField];
	[self._menu setObject:returnValue forKey:@"listCount"];
}

- (void)layoutSubviews
{
	[super layoutSubviews];
	self.imageView.frame = CGRectMake(15, 7, 32, 32);
	self.textLabel.frame = CGRectMake(60, 4, 210, 32);
}

- (void)dealloc {
	[[NSNotificationCenter defaultCenter] postNotificationName:@"viewControllerDealloc" object:self];
	[_menu release];
	[_jsonDao release];
	[super dealloc];
}
@end
